angular.module('starter')
    .controller('orderstateCtrl', function($scope,meilvFac,$ionicScrollDelegate,$stateParams,cfg) {


        $(".order-state p").click(function() {
            $(this).addClass('actived');
            $(this).siblings('p').removeClass('actived');
        })
        $scope.tab = $stateParams.order?$stateParams.order:0;
        $scope.jumpTab = function(index) {
            $scope.tab = index;
            switch ($scope.tab) {
                case 0:
                    $ionicScrollDelegate.scrollTop();
                    $scope.intiOrderDetail(0);
                    break;
                case 1:
                    $ionicScrollDelegate.scrollTop();
                    $scope.intiOrderDetail(1);
                    break;
                case 2:
                    $ionicScrollDelegate.scrollTop();
                    $scope.intiOrderDetail(2);
                    break;
                case 3:
                    $ionicScrollDelegate.scrollTop();
                    $scope.intiOrderDetail(3);
                    break;
            }
        }
        $scope.intiOrderDetail=function(){
            $(".order-state p").eq($scope.tab).addClass('actived');
            $(".order-state p").eq($scope.tab).siblings('p').removeClass('actived');
            // $scope.tab=null;
            meilvFac.order.orderget(
            {type:$scope.tab},
            function(data){
                $scope.orderState=data.info;
            },
            function(data){
                alert(data.info)
            }
        )
        }

        $scope.orderUser=function(){
            meilvFac.order.orderget(
                {
                    type:-1,
                    order_id:$stateParams.id
                },
                function(data){
                    $scope.order=data.info[0];
                },
                function(data){
                    alert(data.info);
                }
            )
        }

        $scope.cancelOrder = function(order){
            if(!confirm('您确定要取消该订单吗？'))
                return;
            meilvFac.order.cancel({
                order_id: order.id
            },function(data){
                alert(data.info);
               $scope.intiOrderDetail($scope.tab); 
               $scope.orderUser();
            },function(data){
                alert(data.data.info);
            });
        };

        $scope.deleteOrder = function(order){
            if(!confirm('您确定要删除该订单吗？'))
                return;
            meilvFac.order.delete({
                order_id: order.id
            },function(data){
                alert(data.info);
               $scope.intiOrderDetail($scope.tab);
               $scope.orderUser(); 
            },function(data){
                alert(data.data.info);
            });
        };

        $scope.pay = function(order){
            meilvFac.order.wxpay({
              order_id: order.id
            },function(response){
              var res = response;
              if(res.info){
                alert(res.info);
                return;
              }
              // console.log(res.redirect);
              var jsApiCall = function(data){
                WeixinJSBridge.invoke(
                  'getBrandWCPayRequest',
                  data,
                  function(res){
                    WeixinJSBridge.log(res.err_msg);
                    // alert(res.err_code+res.err_desc+res.err_msg);
                    if (res.err_msg == 'get_brand_wcpay_request:ok') {
                      alert("支付成功!");
                      $state.go('orderstate',{order:2});
                    }else if (res.err_msg == 'get_brand_wcpay_request:cancel') {
                      alert("已取消支付!");
                    }else if (res.err_msg == 'get_brand_wcpay_request:fail') {
                      alert("支付失败!");
                    }else{
                      alert('支付失败'+res.err_msg);
                    };
                  }
                );
              }
              jsApiCall(res);
            },function(data){
              alert(data.data.info);
            });
          };

        $scope.ticketType = function(type){
            switch(type){
              case '1': return '成人票';
              case '2': return '儿童票';
              case '3': return '老人票';
              case '4': return '成人两日联票';
              case '5': return '儿童两日联票';
              case '6': return '老人两日联票';
              default: return;
            }
        };

        $scope.gotoUserdetail = function(id){
            window.location.href = cfg.baseUrl +'home/index/gotoUserdetail/id/' + id;
        };
    })
